Ext.namespace('GBIF');
Ext.namespace('GBIF.GeoSpatial');

GBIF.GeoSpatial.WFSTDataGridPanel = function(config){

	var store = new Ext.data.JsonStore({
			fields: [
					{name:'name', type:'string',mapping:'properties.name'}
				,	{name:'id', type:'string'}
				,	{name:'state', type:'string'}
        	]
	}); 

	Ext.apply(this,config,{
				    title: 'WFS-T Grid'
				,	border:false
				,	width:400
				,	split:true
				,	layer:''
				,	store:store
				,	id:'grid'
				,	sm: new Ext.grid.RowSelectionModel({singleSelect:true})
				,   columns: [
									{header: "Name", dataIndex:'name',editor: new Ext.form.TextField(),markDirty: false}
								,	{header: "id", dataIndex:'id',autoWidth:true}
								,	{header: "state", dataIndex:'state'}
							]
				,	tbar:[{
								text:'Commit'
							,	ref:'../commit'
						}]
				 , listeners: {
							 	 rowcontextmenu: this.rightClickMenu
 							} 			
			});

	GBIF.GeoSpatial.WFSTDataGridPanel.superclass.constructor.call(this, config)
}
Ext.extend(GBIF.GeoSpatial.WFSTDataGridPanel, Ext.grid.EditorGridPanel, {
		 rightClickMenu: function(grid, row, e){
				 grid.getSelectionModel().selectRow(row);
				 this.fireEvent('rowClick',grid,row,this);
				 var record = grid.getSelectionModel().getSelected().data;
				 var items = [];
				 items.push({
					 	text: 'Delete Feature'
				 	, 	scope: this
				 	, 	handler: function() {
									Ext.MessageBox.confirm("Confirm","Are you Sure?", this.deleteNode);
								}
				 		},{
					 	text: 'Center On Map'
				 	, 	scope: this
				 	, 	handler: function() {
									var rec = grid.store.getAt(row);
									var lat = rec.json.geometry.coordinates[0].lat;
									var lon = rec.json.geometry.coordinates[0].lon;
									this.fireEvent("centerToMap",lon,lat,this);
								}
				 		});
				
				 var menu = new Ext.menu.Menu({
				 		items: items
				 });
				 var xy = e.getXY();
				 menu.showAt(xy);
	 		}
,	deleteNode:function(btn, text){
		if (btn == 'yes') {
				var grid = Ext.getCmp('grid');
				grid.fireEvent('deleteFeature',this);
			}
	}			 
});
